package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module03.ArticleCares;
import com.teamwork.common.result.Result;
import com.teamwork.module03.mapper.ArticleCaresMapper;
import com.teamwork.module03.service.ArticleCaresService;
import com.teamwork.module03.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ArticleCaresServiceImpl extends ServiceImpl<ArticleCaresMapper, ArticleCares> implements ArticleCaresService {

//    @Resource
//    private ArticleCaresMapper articleCaresMapper;
    /**
     * 收藏文章
     * @param userid
     * @param artid
     */
    @Override
    public boolean InsertOneByUserId(Integer userid, Integer artid) {

        //String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        LambdaQueryWrapper<ArticleCares> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCares::getUserId,userid).eq(ArticleCares::getArticleId,artid);
        ArticleCares one = this.getOne(wrapper);
        if(one!=null){
            //该用户已经收藏了该文章，不能在收藏了
            return false;
        }
         //该用户没有收藏过，可以收藏
        ArticleCares articleCares = new ArticleCares();
        articleCares.setUserId(userid);
        articleCares.setArticleId(artid);
        boolean b = this.save(articleCares);
        if(b){
            log.info("用户id:{} 收藏了文章id:{}",userid,artid);
            return true;
        }else {
            log.error("用户id:{} 收藏了文章id:{}失败！！！",userid,artid);
            return false;
        }


    }

    /**
     * 根据文章id和用户id查询
     * @param userid
     * @param artid
     * @return
     */
    @Override
    public ArticleCares getByUidAndAid(Integer userid, Integer artid) {
        LambdaQueryWrapper<ArticleCares> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCares::getUserId,userid).eq(ArticleCares::getArticleId,artid);

        return this.getOne(wrapper);
    }

    @Override
    public boolean deleteOneByUidAndAid(Integer userid, Integer artid) {
        LambdaQueryWrapper<ArticleCares> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCares::getUserId,userid).eq(ArticleCares::getArticleId,artid);
        return this.remove(wrapper);
    }

    /**
     * 获取用户收藏的文章id 集合
     * @param userid 用户id
     * @return
     */
    @Override
    public List<Integer> getMyCaresArtIds(Integer userid) {

        LambdaQueryWrapper<ArticleCares> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCares::getUserId,userid);

        List<ArticleCares> list = this.list(wrapper);


        List<Integer> staraids = list.stream().map(ArticleCares::getArticleId).collect(Collectors.toList());


        return staraids;
    }
}
